from sqlalchemy import Boolean, Column, Integer, String, ForeignKey, DateTime, Text, Float
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime

Base = declarative_base()

class BaseDictModel(Base):
    __abstract__ = True

    # created_at = Column(DateTime, default=datetime.now)

    def to_dict(self):
        return {}
    
    # @created_at.getter
    # def created_at(self):
    #     return self.created_at

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    is_active = Column(Boolean, default=True)
    is_admin = Column(Boolean, default=False)
    points = Column(Integer, default=0)
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)

    emails = relationship("Email", back_populates="user")
    subscriptions = relationship("UserSubscription", back_populates="user")
    point_logs = relationship("PointLog", back_populates="user")


class Email(Base):
    __tablename__ = "emails"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    is_primary = Column(Boolean, default=False)
    created_at = Column(DateTime, default=datetime.now)

    user = relationship("User", back_populates="emails")

class Subscription(Base):
    __tablename__ = "subscriptions"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, unique=True, index=True)
    description = Column(Text)
    email_subject = Column(String)  # 新增邮件主题字段
    message_template = Column(Text)
    message_params = Column(Text)
    points_cost = Column(Integer, default=0)
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)

    users = relationship("UserSubscription", back_populates="subscription")

class UserSubscription(Base):
    __tablename__ = "user_subscriptions"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    subscription_id = Column(Integer, ForeignKey("subscriptions.id"))
    created_at = Column(DateTime, default=datetime.now)

    user = relationship("User", back_populates="subscriptions")
    subscription = relationship("Subscription", back_populates="users")

class PointLog(Base):
    __tablename__ = "point_logs"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    points_change = Column(Integer)
    reason = Column(String)
    created_at = Column(DateTime, default=datetime.now)

    user = relationship("User", back_populates="point_logs")

class AdminEmail(Base):
    __tablename__ = "admin_emails"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String, unique=True, index=True)
    smtp_server = Column(String)
    smtp_port = Column(Integer)
    username = Column(String)
    password = Column(String)
    is_default = Column(Boolean, default=False)
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)

class EmailLog(Base):
    __tablename__ = "email_logs"

    id = Column(Integer, primary_key=True, index=True)
    from_email = Column(String)
    to_email = Column(String)
    subject = Column(String)
    content = Column(Text)
    status = Column(String)
    error_message = Column(Text, nullable=True)
    created_at = Column(DateTime, default=datetime.now)

class APILog(Base):
    __tablename__ = "api_logs"

    id = Column(Integer, primary_key=True, index=True)
    endpoint = Column(String)
    method = Column(String)
    params = Column(Text)
    status_code = Column(Integer)
    response_time = Column(Float)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    error_message = Column(Text, nullable=True)
    created_at = Column(DateTime, default=datetime.now)

    user = relationship("User")
